﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.PhysicalexaminationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.PhysicalexaminationHarder
{
    public class FtPhysicalexaminationHarder:IRequestHandler<FtPhysicalexaminationCommand, APIResult<PhysicalexaminationDto>>
    {


        private readonly IBaseRepository<Physicalexamination> Physicalexaminationrepository;
        private readonly IBaseRepository<Patient> Patientrepository;
        private readonly IBaseRepository<HealthCheckPackage> HealthCheckPackagerepository;

        public FtPhysicalexaminationHarder(IBaseRepository<Physicalexamination> physicalexaminationrepository, IBaseRepository<Patient> patientrepository, IBaseRepository<HealthCheckPackage> healthCheckPackagerepository)
        {
            Physicalexaminationrepository = physicalexaminationrepository;
            Patientrepository = patientrepository;
            HealthCheckPackagerepository = healthCheckPackagerepository;
        }

        public Task<APIResult<PhysicalexaminationDto>> Handle(FtPhysicalexaminationCommand request, CancellationToken cancellationToken)
        {
            APIResult<PhysicalexaminationDto> result = new APIResult<PhysicalexaminationDto>();
            result.Code = APIEnum.Mvc;
            result.Msg = "查询成功";
            var list = from a in Physicalexaminationrepository.GetAll()
                       join b in Patientrepository.GetAll() on a.PatientId equals b.PatientId
                       join c in HealthCheckPackagerepository.GetAll() on a.HealthCheckPackageId equals c.HealthCheckPackageId
                       where a.IsDeleted == false && a.physicalId == request.physicalId
                       select new PhysicalexaminationDto
                       {

                           physicalId = a.physicalId,

                           /// <summary>
                           /// 体检人
                           /// </summary>
                           PatientId = b.PatientId,

                           /// 
                           /// <summary>
                           /// 关系
                           /// </summary>
                           Impact = b.Impact,
                           /// <summary>
                           /// 姓名
                           /// </summary>
                           PatientName = b.PatientName,
                           /// <summary>
                           /// 证件类型
                           /// </summary>
                           DocumentType = b.DocumentType,
                           /// <summary>
                           /// 证件号码（身份证号）
                           /// </summary>
                           DocumentNo = b.DocumentNo,
                           /// <summary>
                           /// 就诊卡号
                           /// </summary>
                           PatientNo = b.PatientNo,
                           /// <summary>
                           /// 手机号码
                           /// </summary>
                           Phone = b.PatientNo,
                           /// <summary>
                           /// 就诊人地址（选填）
                           /// </summary>
                           Address = b.Address,
                           /// <summary>
                           /// 设为默认就诊人
                           /// </summary>
                           IsDefault = b.IsDefault,
                           /// <summary>
                           /// 绑定Id
                           /// </summary>
                           ParentId = b.ParentId,

                           /// <summary>
                           /// 医院
                           /// </summary>
                           Hospital = a.Hospital,
                           /// <summary>
                           /// 地址
                           /// </summary>
                           Address2 = a.Address,

                           /// <summary>
                           /// 科室
                           /// </summary>
                           Department = a.Department,

                           /// <summary>
                           /// 预约体检时间
                           /// </summary>
                           PhysicalTime = a.PhysicalTime,

                           /// <summary>
                           /// 候诊时间
                           /// </summary>
                           WaitingTime = a.WaitingTime,

                           /// <summary>
                           /// 套餐
                           /// </summary>
                           HealthCheckPackageId = c.HealthCheckPackageId,


                           /// <summary>
                           /// 体检套餐名称
                           /// </summary>
                           PackageName = c.PackageName,

                           PackageType = c.PackageType,
                           /// <summary>
                           /// 价格
                           /// </summary>
                           Medicalexaminationfee = c.PackagePrice,

                           /// <summary>
                           /// 预约时间
                           /// </summary>
                           bookingTime = a.bookingTime,

                           /// <summary>
                           /// 状态
                           /// </summary>
                           State = a.State,

                           Checktime=a.Checktime,
                           /// <summary>
                           /// 备注
                           /// </summary>
                           Remark = a.Remark


                       };
            result.Data= list.First();
           return Task.FromResult(result);


        }
    }
}
